﻿@model Dyd.BaseService.ConfigManager.Domain.Model.tb_config_model
@using Dyd.BaseService.ConfigManager.Domain.Model

@{
    ViewBag.Title = "修改配置";
    Layout = "~/Views/Shared/_Layout.cshtml";
    List<tb_failover_model> FailOverList = ViewBag.FailOverList as List<tb_failover_model>;
    List<tb_loadbalance_model> LoadBalanceList = ViewBag.LoadBalanceList as List<tb_loadbalance_model>;
}
<div class="head">
    <div class="top">
        <h3 class="fl">@ViewBag.Title</h3>
        <a href="javascript:void(0);" class="back fr">返回</a>
    </div>
</div>
<div>
    @using (Html.BeginForm("ConfigAdd", "Config"))
    {
        @Html.AntiForgeryToken()
        <ul class="mydetail">
            <li>
                @Html.LabelFor(model => model.categoryid)
                @Html.DropDownListFor(m => m.categoryid, ViewBag.Category as List<SelectListItem>)
                @Html.ValidationMessageFor(model => model.categoryid)
            </li>

            <li>
                @Html.LabelFor(model => model.configkey)
                @Html.EditorFor(model => model.configkey)
                @Html.ValidationMessageFor(model => model.configkey)
            </li>

            <li>
                @Html.LabelFor(model => model.configvalue)
                @Html.TextAreaFor(model => model.configvalue, 8, 30, new { })
                @Html.ValidationMessageFor(model => model.configvalue)
            </li>
            <li>
                @Html.LabelFor(model => model.remark)
                @Html.TextAreaFor(model => model.remark, 8, 30, new { })
                @Html.ValidationMessageFor(model => model.remark)
            </li>
            <li>
                @Html.LabelFor(model => model.loadbalancealgorithmenum)
                <input id="loadBalanceSwitch" type="checkbox" name="loadBalanceSwitch" @(Model.loadbalancealgorithmenum > 0 ? "checked" : "") />
            </li>
            <li id="loadBalanceLi" class="loadBalanceLi" @(Model.loadbalancealgorithmenum > 0 ? "" : "style=display:none;")>

                <label></label><span id="addLoadbalanceRow" class="btn3" style="color:red; cursor:pointer">添加</span>
                <div style="width:600px;margin-left:185px;">

                    <label></label>
                    <div class="loadbalance">
                        @foreach (var item in LoadBalanceList)
                        {
                            <div class='loadbalanceRow'><input class='value' placeholder='地址' title="地址" style='width:300px;' type='text' value='@item.value' /><input class='boostpercent' placeholder='权重' title="权重" style='width:100px;' type='text' value='@item.boostpercent' /><img class='moveUp' style='width:12px;height:12px;cursor:pointer' src='/Content/images/gtk_go_up.png'><img class='erase' style='width:12px;height:12px;cursor:pointer' src='/Content/images/erase.png'></div>
                        }
                    </div>
                </div>
            </li>
            <li>
                @Html.LabelFor(model => model.failoversequence,"故障转移")
                <input id="failOverSwitch" type="checkbox" name="failOverSwitch" @(Model.failoversequence >= 0 ? "checked" : "") />
            </li>
            <li id="failOverLi" @(Model.failoversequence >= 0 ? "" : "style=display:none;")>
                <label></label><span id="addFailLoadRow" class="btn3" style="color:red;cursor:pointer">添加</span> <span id="failLoadCheckedClear" class="btn3" style="color:red; cursor:pointer">取消选择</span>
                <div style="width:600px;margin-left:190px;">
                    <label></label>
                    <div class="failLoad">
                        @foreach (var item in FailOverList)
                        {
                            <div class='failLoadRow'><input class='failLoadCheck' name='failLoadCheck' type='radio' @(item.sequence == Model.failoversequence ? "checked" : "") /><input class='value' placeholder='地址' title="地址" style='width:300px;' type='text' value='@item.value' /><img class='moveUp' style='width:12px;height:12px;cursor:pointer' src='/Content/images/gtk_go_up.png'><img class='erase' style='width:12px;height:12px;cursor:pointer' src='/Content/images/erase.png'></div>
                        }
                    </div>

                </div>
            </li>
            <li>
                <input id="save" type="button" class="btn1" value="提交并通知相关客户端重启" />
                @Html.ValidationMessage("Error")
            </li>
        </ul>
    }
</div>
@section Scripts{
    <script>
        $(function () {

            $("#loadBalanceSwitch").on("click", function () {

                $("#loadBalanceLi").toggle();
            });
            $("#failOverSwitch").on("click", function () {

                $("#failOverLi").toggle();
            });

            $("#failLoadCheckedClear").on("click", function () {
                $("input:radio[name='failLoadCheck']").attr("checked",false);
            });
            
            $("body").on("click", ".moveUp", function () {
                var insertBefore = $(this).parent().prev();
                $(this).parent().insertBefore(insertBefore);
            });

            $("body").on("click", ".erase", function () {
                $(this).parent().remove();
            });
            

            $("#addLoadbalanceRow").on("click", function () {
                var routeValuesHtml = "<div class='loadbalanceRow'><input class='value' title='地址' placeholder='地址' style='width:300px;' type='text' value=''/><input class='boostpercent' placeholder='权重' title='权重' style='width:100px;' type='text' value='10'/><img class='moveUp' style='width:12px;height:12px;cursor:pointer' src='/Content/images/gtk_go_up.png'><img class='erase' style='width:12px;height:12px;cursor:pointer' src='/Content/images/erase.png'></div>"
                $(".loadbalance").append(routeValuesHtml);
            });

            $("#addFailLoadRow").on("click", function () {
                var routeValuesHtml = "<div class='failLoadRow'><input class='failLoadCheck' name='failLoadCheck' type='radio' /><input class='value' placeholder='地址' style='width:300px;' type='text' value=''/><img class='moveUp' style='width:12px;height:12px;cursor:pointer' src='/Content/images/gtk_go_up.png'><img class='erase' style='width:12px;height:12px;cursor:pointer' src='/Content/images/erase.png'></div>"
                $(".failLoad").append(routeValuesHtml);
            });

            $("#save").click(function () {

                if (!$("form").valid()) {
                    return;
                }
                var loadbalance = "[";
                $(".loadbalanceRow").each(function (j) {
                    loadbalance += "{";
                    loadbalance += '"value":"' + $.trim($(this).children(".value").val()) + '","boostpercent":"' + $.trim($(this).children(".boostpercent").val()) + '"';
                    loadbalance += "},";
                });
                if (loadbalance.length > 1) {
                    loadbalance = loadbalance.substring(0, loadbalance.length - 1);
                }
                loadbalance += "]";

                var failLoad = "[";
                $(".failLoadRow").each(function (j) {
                    var checkedIndex = $.trim($(this).children(".failLoadCheck").attr("checked")) == "checked" ? j + 1 : 0;
                    failLoad += "{";
                    failLoad += '"value":"' + $.trim($(this).children(".value").val()) + '","id":"' + checkedIndex + '"';
                    failLoad += "},";
                });
                if (failLoad.length > 1) {
                    failLoad = failLoad.substring(0, failLoad.length - 1);
                }
                failLoad += "]";


                var submitJson = $("form").serialize();
                $.ajax({
                    url: '@Url.Action("ConfigEdit", "Config")',
                    data: submitJson + "&loadBalance=" + loadbalance + "&failLoad=" + failLoad,
                    type: "post",
                    success: function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.href = "/Config/ConfigIndex";
                        } else {
                            alert(data.Message);
                        }
                    }
                });
            });
        });

    </script>
}